<?php

namespace App\Http\Controllers\Admin;

use App\Repositories\Eloquent\RoleRepositoryEloquent;
use App\Repositories\Eloquent\PermissionRepositoryEloquent;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RoleController extends Controller
{
    public $role ;
    protected $permission;
    public function __construct(RoleRepositoryEloquent $role,PermissionRepositoryEloquent $permission)
    {
        $this->role = $role;
        $this->permission = $permission;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $roles =$this->role->orderBy('id','desc')->paginate('10');
        return view('vendor.admin.roles',compact('roles'));
    }

    public function ajaxIndex()
    {
        // datatables请求次数
        $draw = request('draw', 1);
        // 开始条数
        $start = request('start', 0);
        // 每页显示数目
        $length = request('length', 10);
        // datatables是否启用模糊搜索
        $search['regex'] = request('search.regex', false);
        // 搜索框中的值
        $search['value'] = request('search.value', '');
        // 排序
        $order['name'] = request('columns.' .request('order.0.column',0) . '.name');
//        $order['dir'] = request('order.0.dir','asc');

        $result = $this->role->getRoleList($start,$length,$search,$order);

        $roles = [];

        if ($result['roles']) {
            foreach ($result['roles'] as $v) {
                $v->actionButton = true;
                $roles[] = $v;
            }
        }
        $data =  [
            'draw' => $draw,
            'recordsTotal' => $result['count'],
            'recordsFiltered' => $result['count'],
            'data' => $roles,
        ];
        return response()->json($data);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('vendor.admin.create',
            [
                'permissions' => $this->permission->all(),
                'rolePermissions' => array()
            ]
        );
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $this->role->create($request->all());
        return redirect('admin/role')->withSuccess('添加成功！');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $role = $this->role->find($id);
        return view('vendor.admin.edit',[
            'role' => $role,
            'rolePermissions' => $role->perms->pluck('id')->all(),
            'permissions' => $this->permission->all()
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $this->role->update($request->all(), $id);
        return redirect('admin/role')->withSuccess('更新角色成功！');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $this->role->destroy($id);

        return redirect('admin/role')->withSuccess('删除成功！');
    }
}
